﻿Imports System
Imports System.Data
Imports System.Data.OracleClient

Imports System.Configuration
Imports System.Windows.Forms
Imports System.IO
Structure appSettings
    Dim ws_url As String
    Dim ws_user As String
    Dim ws_pwd As String
    Dim mo_tmrInt As Integer
    Dim rootdir As String
    Dim modir As String
    Dim mobkdir As String
    Dim mt_tmrInt As Integer
    Dim mc_tmrInt As Integer
    Dim mcdir As String
    Dim mcbkdir As String

    Dim logdir As String
    Dim ConnectionString As String
End Structure
Module sms_mdlMain
    Public oradbConnection As OracleConnection
    Public smsSettings As appSettings
    Public _today As String = Format(Date.Now, "yyyyMMdd")
    Public _smsRootdir As String
    Public Sub Main()
        smsSettings = getAppSettings()
        _smsRootdir = mkDir(smsSettings.rootdir)

        If Not oraDBConnect() Then
            MsgBox("Database connection failed, pls check", MsgBoxStyle.Critical)
            End
        End If
        If Not prevAppInstance() Then
            Dim appwindow As New sms_frmMain
            System.Windows.Forms.Application.Run(appwindow)
        End If
    End Sub
    Private Function oraDBConnect() As Boolean
        oradbConnection = New OracleConnection(smsSettings.ConnectionString)
        oradbConnection.Open()
        If oradbConnection.State = ConnectionState.Open Then
            Return True
        Else
            Return False
        End If
    End Function
    Public Sub oradbClose()
        If Not oradbConnection.State = ConnectionState.Closed Then
            oradbConnection.Dispose()
            oradbConnection.Close()
            oradbConnection = Nothing
        End If
    End Sub
    Private Function getAppSettings() As appSettings
        Dim result As New appSettings
        With result
            .mo_tmrInt = ConfigurationManager.AppSettings.Item("moTmrInt")
            .mt_tmrInt = ConfigurationManager.AppSettings.Item("moTmrInt")
            .mc_tmrInt = ConfigurationManager.AppSettings.Item("mcTmrInt")
            .rootdir = ConfigurationManager.AppSettings.Item("rootdir")
            .modir = ConfigurationManager.AppSettings.Item("modir")
            .mobkdir = ConfigurationManager.AppSettings.Item("mobkdir")
            .mcdir = ConfigurationManager.AppSettings.Item("mcdir")
            .mcbkdir = ConfigurationManager.AppSettings.Item("mcbkdir")
            .logdir = ConfigurationManager.AppSettings.Item("logdir")
            .ConnectionString = ConfigurationManager.AppSettings.Item("oradb")
            .ws_url = ConfigurationManager.AppSettings.Item("WebService")
            .ws_user = ConfigurationManager.AppSettings.Item("wsUser")
            .ws_pwd = ConfigurationManager.AppSettings.Item("wsPwd")
        End With
        Return result
    End Function
    Private Function prevAppInstance() As Boolean
        prevAppInstance = False
        Dim appName As String = Process.GetCurrentProcess.ProcessName
        Dim sameProcessTotal As Integer = Process.GetProcessesByName(appName).Length
        If sameProcessTotal > 1 Then
            Return True
        End If
    End Function
    Public Function mkDir(ByVal directory As String) As String
        If IO.Directory.Exists(directory) Then
            Return directory + "\"
        Else
            IO.Directory.CreateDirectory(directory)
        End If
        Return directory + "\"
    End Function
End Module
